
var CRAWLER_BUILD_CONFIG = [
// | 银行英文简称|入口变量 | 版本 | 统一入口| PC或WAP           | 是否IE11  | lua脚本名称            | 依赖HttpClient | 启用http代理 | 依赖LuaClient| 启用LuaClient代理| 启用锁
    [ PSBC,     "pc1",    "v4",  true,    EntranceType.PC,    false,      "psbc",               true,           true,         true,          true,          false ],
    //wap2和wap完全相同只是入口变量不同
    [ CMB,      "wap2",   "v4",  false,   EntranceType.WAP,   false,      "",                   true,            true,         false,         true,          false ],
    [ CMB,      "pc1",    "v4",  true,    EntranceType.PC,    false,      "cmb",                true,            true,         true,          true,          false ],
    [ CCB,      "pc1",    "v4",  true,    EntranceType.PC,    false,      "ccb",                false,           true,         true,          true,          false ],
    [ CGB,      "pc1",    "v6",  true,    EntranceType.PC,    false,      "cgb",                false,           true,         true,          true,          true  ],
    [ COMM,     "wap",    "v4",  false,   EntranceType.WAP,   true,       "comm",               true,            true,         true,          true,          false ],
    [ COMM,     "pc1",    "v4",  true,    EntranceType.PC,    true,       "comm",               true,            true,         true,          true,          false ],
    [ CMBC,     "pc1",    "v4",  true,    EntranceType.PC,    false,      "cmbc",               false,           true,         true,          true,          false ],
    [ CITIC,    "pc1",    "v6",  false,   EntranceType.PC,    true,       "citic_credit",       false,           true,         true,          true,          false ],
    [ CITIC,    "pc2",    "v6",  false,   EntranceType.PC,    true,       "citic_credit",       false,           true,         true,          true,          false ],
    [ CITIC,    "pc3",    "v6",  false,   EntranceType.PC,    true,       "citic_debit",        false,           true,         true,          true,          false ],
    [ CIB,      "wap",    "v6",  false,   EntranceType.WAP,   false,      "",                   true,            true,         false,         true,          false ],
    [ BOC,      "wap",    "v4",  false,   EntranceType.WAP,   false,      "",                   true,            true,         false,         true,          false ],
    [ BOC,      "pc1",    "v4",  true,    EntranceType.PC,    false,      "boc",                true,            true,         true,          true,          false ],
    [ ICBC,     "pc1",    "v6",  true,    EntranceType.PC,    true,       "icbc",               false,           true,         true,          true,          false ],
    [ CEB,      "pc1",    "v4",  false,   EntranceType.PC,    false,      "",                   true,            true,         false,         true,          false ],
    [ CEB,      "pc2",    "v4",  true,    EntranceType.PC,    false,      "ceb",                false,           true,         true,          true,          false ],
    [ PINGAN,   "wap",    "v4",  false,   EntranceType.WAP,   false,      "pingan_wap",         true,            true,         true,          true,          false ],
    [ PINGAN,   "pc1",    "v4",  false,   EntranceType.PC,    true,       "pingan_credit_pc1",  false,           true,         true,          true,          false ],
    [ PINGAN,   "pc2",    "v4",  false,   EntranceType.PC,    true,       "pingan",             false,           true,         true,          true,          false ],
    [ PINGAN,   "pc3",    "v5",  false,   EntranceType.PC,    true,       "pingan_credit_pc3",  false,           true,         true,          true,          false ],
    [ PINGAN,   "pc4",    "v5",  false,   EntranceType.PC,    true,       "pingan_debit_pc4",   false,           true,         true,          true,          false ],
    [ HXB,      "pc1",    "v4",  true,    EntranceType.PC,    false,      "hxb",                false,           true,         true,          true,          false ],
    [ BEIJING,  "pc1",    "v4",  false,   EntranceType.PC,    false,      "beijing_credit",     false,           true,         true,          true,          false ],
    [ BEIJING,  "pc2",    "v4",  false,   EntranceType.PC,    false,      "beijing_debit",      false,           true,         true,          true,          false ],
    [ SHANGHAI, "pc1",    "v6",  true,    EntranceType.PC,    false,      "shanghai",           false,           true,         true,          true,          false ],
    [ ABC,      "pc1",    "v4",  true,    EntranceType.PC,    false,      "abc",                false,           true,         true,          true,          true  ],
    [ SPD,      "pc1",    "v4",  false,   EntranceType.PC,    false,      "spd_credit",         true,            true,         true,          true,          false ],
    // [ SPD,      "pc2",    "v2",  false,   EntranceType.PC,    true,       "spd",                false,           true,         true,          true,          false ],
    [ SPD,      "pc3",    "v4",  true,    EntranceType.PC,    false,      "spd",                false,           true,         true,          true,          false ],
];

function findCrawlerBuildConfig(){


    var cardType = context.get(LUA_TASK_CARD_TYPE);
    var loginType = context.get(LUA_TASK_LOGIN_TYPE);
    var bankName = context.get(LUA_TASK_BANK_NAME);
    var taskKey = context.get(LUA_TASK_KEY);

    var cardTypeEnum = CardType.stringToCardType(cardType);
    var loginTypeEnum = LoginType.stringToLoginType(loginType);

    var entranceFlag;
    var targetConfig;
    var crawlerBuilderConfig;
    var extendParams = JSON.parseObject(context.get(TASK_JSONTXT));
    if(extendParams.get(TASK_DEBUG) != null && Boolean.valueOf(extendParams.get(TASK_DEBUG))){
        logger.info("任务【 {} 】 开启DEBUG模式", taskKey)
        bankName = TASK_DEBUG
        context.putAll(extendParams);
        context.put(LUA_TASK_BANK_NAME, TASK_DEBUG);
        entranceFlag = "pc1"
        context.put("entrance", entranceFlag)

        context.put("case_name", extendParams.get("case_name"))
        //              | 银行英文简称|入口变量 | 版本    | 统一入口| PC或WAP           | 是否IE11  | lua脚本名称           | 依赖HttpClient | 启用http代理 | 依赖LuaClient| 启用LuaClient代理| 启用锁
        targetConfig = [ "debug",    "pc1",   "debug",  true,    EntranceType.PC,    false,      "",                   true,            true,         false,         true,          false ]

    } else {

        if(!DEV_LEVEL.equals(System.getProperty(PROFILE_PROPERTY))){
            var paramsConfigUrl = String.format(PropertyManager.getString("params_config_url"), "bank_config", bankName.toUpperCase() + "." + cardType.toUpperCase() + "." + loginType.toUpperCase());
            entranceFlag = JSON.parseArray(RequestUtil.getNoProxy(paramsConfigUrl)).get(0).get("param_value");

        } else {
            entranceFlag = System.getProperty("entranceFlag")
            if(StringUtils.isBlank(entranceFlag)) {
                logger.error("任务【 {} 】 无法获取入口标识！请设置变量 -DentranceFlag={xxx} xxx表示入口标识", taskKey)
                throwBankExceptionByErrorCode(BALO_99601_05)
            }
        }

        for each (var config in CRAWLER_BUILD_CONFIG) {
            if(!bankName.equals(config[0])){
                continue;
            }
            if(entranceFlag.equals(config[1])){
                targetConfig = config
                break;
            }
        }

        logger.info("任务【 {} 】 选择入口【 {} 】", taskKey, entranceFlag)
        context.put("entrance", entranceFlag)



    }
    //设置版本
    context.put("version",targetConfig[2]);
    crawlerBuilderConfig = CrawlerBuildConfig.custom()
        .setBankName(bankName)
        .setCardType(cardTypeEnum)
        .setVersion(targetConfig[2])
        .setLoginType(loginTypeEnum)
        .setUnifiedEntrance(targetConfig[3])
        .setEntranceType(targetConfig[4])
        .setIE11(targetConfig[5])
        .setLuaScriptName(targetConfig[6])
        .setNeedHttpClient(targetConfig[7])
        .setEnableHttpProxy(targetConfig[8])
        .setNeedLuaClient(targetConfig[9])
        .setEnableLuaProxy(targetConfig[10])
        .setEnableLock(targetConfig[11])
        .build()
    logger.info("任务【 {} 】 crawlerBuildConfig 【 {} 】", context.get(LUA_TASK_KEY), crawlerBuilderConfig)
    context.put("crawlerBuilderConfig", crawlerBuilderConfig)
}

function runJS() {

    var cardType = context.get(LUA_TASK_CARD_TYPE);
    var loginType = context.get(LUA_TASK_LOGIN_TYPE);
    var bankName =  context.get(LUA_TASK_BANK_NAME);

    var cardTypeEnum = CardType.stringToCardType(cardType);
    var loginTypeEnum = LoginType.stringToLoginType(loginType);


    var isCardTypeMatch = cardTypeEnum.equals(CardType.ALL) || cardTypeEnum.equals(cardTypeEnum);

    var isLoginTypeMatch = loginTypeEnum.equals(LoginType.ALL) || loginTypeEnum.equals(loginTypeEnum);

    if(CIB.equals(bankName)) {
        isLoginTypeMatch = loginTypeEnum.equals(LoginType.IDCARD);
        context.put(LUA_TASK_LOGIN_TYPE, LoginType.CARDNOANDIDCARD.toString());
    } else if(BEIJING.equals(bankName) ){
        if(LoginType.CREDITCARDNO.equals(loginTypeEnum)){
            context.put(LUA_TASK_LOGIN_TYPE, LoginType.CARDNOANDIDCARD.toString());
            isLoginTypeMatch = true;
        }
        // } else if (COMM.equals(bankName)) {//邮箱 手机号 卡号的时候走wap
        //     if (loginTypeEnum.equals(LoginType.MOBILE)) {
        //         var token = context.get(LUA_TASK_TOKEN);
        //         if (token.matches("\\w{6,15}")) {
        //             isLoginTypeMatch = true;
        //         } else {
        //             isLoginTypeMatch = true;
        //         }
        //     }
    } else if(CGB.equals(bankName)||CMBC.equals(bankName)){
        if(loginTypeEnum.equals(LoginType.CARDNO)){
            if(cardTypeEnum.equals(CardType.CREDITCARD)){
                loginTypeEnum = LoginType.CREDITCARDNO;
                context.put(LUA_TASK_LOGIN_TYPE,"CREDITCARDNO")
                context.put(LUA_TASK_CREDIT_CARD_NO,context.get(LUA_TASK_CARDNO))
                isLoginTypeMatch = true;
            }else if(cardTypeEnum.equals(CardType.DEBITCARD)){
                loginTypeEnum = LoginType.DEBITCARDNO;
                context.put(LUA_TASK_LOGIN_TYPE,"DEBITCARDNO")
                context.put(LUA_TASK_DEBIT_CARD_NO,context.get(LUA_TASK_CARDNO))
                isLoginTypeMatch = true;
            }
        }
    } else if(SHANGHAI.equals(bankName)) {
        if(loginTypeEnum.equals(LoginType.CREDITCARDNO)) {
            print(context)
            context.put(LUA_TASK_LOGIN_TYPE,"CARDNO")
            context.put(LUA_TASK_CARDNO,context.get(LUA_TASK_CREDIT_CARD_NO))
            isLoginTypeMatch = true;
        }else if(loginTypeEnum.equals(LoginType.DEBITCARDNO)){
            context.put(LUA_TASK_LOGIN_TYPE,"CARDNO")
            context.put(LUA_TASK_CARDNO,context.get(LUA_TASK_DEBIT_CARD_NO))
            isLoginTypeMatch = true;
        }

    }


    if(isCardTypeMatch && isLoginTypeMatch){
        findCrawlerBuildConfig()
    } else {
        var taskKey = context.get(LUA_TASK_KEY);
        logger.info("任务【 {} 】未能找到配置", taskKey)
    }

}